﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using WeddingEventManagement_API;
using WeddingEventManagementApp.Models;

namespace WeddingEventManagementApp.Controllers
{
#if !DEBUG
    [RequireHttpsAttribute]
#endif
    public class BaseController : Controller
    {
        [AllowAnonymous]
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {

            // Work only for GET request
            //if (filterContext.RequestContext.HttpContext.Request.RequestType != "GET")
            //return;

            // Do not work with AjaxRequests
            if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
                return;

            if (User.Identity.Name == null)
                return;

            var user = Membership.GetUser(User.Identity.Name);

            if (user == null)
                return;

            var api = new SettingsManager();

            // Load Settings
            filterContext.Controller.ViewBag.CompanyName = api.GetSetting("_appCompanyName").value;
            filterContext.Controller.ViewBag.CompanyAddress = api.GetSetting("_appCompanyAddress").value;
            filterContext.Controller.ViewBag.CompanyCity = api.GetSetting("_appCompanyCity").value;
            filterContext.Controller.ViewBag.CompanyProvince = api.GetSetting("_appCompanyProvince").value;
            filterContext.Controller.ViewBag.CompanyPostal = api.GetSetting("_appCompanyPostal").value;
            filterContext.Controller.ViewBag.CompanyPhone = api.GetSetting("_appCompanyPhone").value;
            filterContext.Controller.ViewBag.CompanyFax = api.GetSetting("_appCompanyFax").value;
            filterContext.Controller.ViewBag.CompanyLogo = api.GetSetting("_appCompanyLogo").value;
            filterContext.Controller.ViewBag.CompanyEmail = api.GetSetting("_appCompanyEmail").value;
            filterContext.Controller.ViewBag.TaxRate = api.GetSetting("_appTaxRate").value;

            filterContext.Controller.ViewBag.Username = user.UserName;
            filterContext.Controller.ViewBag.MainGravatar = (user.Email == null ? "" : Utility.MD5Hash(user.Email));
            filterContext.Controller.ViewBag.FooterHTML = api.GetSetting("_appFooterCode").value;
            filterContext.Controller.ViewBag.PrimaryColor = api.GetSetting("_appPrimaryColor").value;
            filterContext.Controller.ViewBag.SecondaryColor = api.GetSetting("_appSecondaryColor").value;
        }

        protected void ShowMessage(string title, string message, MessageType type)
        {
            TempData["Notify"] = message;
            TempData["NotifyTitle"] = title;
            TempData["NotifyType"] = type.ToString();
        }

        protected void ShowMessage(string message, MessageType type)
        {
            TempData["Notify"] = message;
            TempData["NotifyType"] = type.ToString();
        }

    }
}
